<?php
class AdminController extends StoreAppController {
  	var $name = 'Store';
	var $uses = array('Store.StoreProduct','Store.StoreUpload');
	
	var $permissions = array(
		'add' => array('admin','manag'),
		'edit' => array('admin','manag'),
		'delete' => array('admin','manag')
	);


	public function beforeFilter(){
		parent::beforeFilter();
		$this->layout = 'cms';
	}

	public function display(){
	}
	public function index(){	    
	}
	
	function add($group_id=null){	
		
		if(!empty($this->data)){
			if($this->FileUpload->success){
				$this->data['StoreProduct']['upload_id'] = $this->StoreUpload->getLastInsertID();
				$this->data['StoreProduct']['added'] = date('Y-m-d H:i:s');
				
				$lp = $this->StoreProduct->find('first',array('conditions'=>array('StoreProduct.group_id'=>$this->data['StoreProduct']['group_id']),'fields'=>array('MAX(StoreProduct.lp) AS lp'), 'group' => 'StoreProduct.group_id'));
				$this->data['StoreProduct']['lp'] = $lp['0']['lp']+1;
				
				$this->StoreProduct->create();
				if ($this->StoreProduct->save($this->data)) {
					$this->Session->setFlash(__d('store','The picture has been saved', true));
				} else {
					$this->Session->setFlash(__d('store','The picture could not be saved. Please, try again.', true));
				}				 
			}else{
				$this->Session->setFlash($this->FileUpload->showErrors());
			}
			$this->redirect($this->referer());
		}
		$this->data['StoreProduct']['group_id'] = (is_null($group_id)) ? $this->data['StoreProduct']['group_id']:$group_id;
	}
	
	function edit($id=null){
		if(!empty($this->data)){
			if ($this->StoreProduct->save($this->data)) {
				$this->Session->setFlash(__d('store','The picture has been saved', true));
			} else {
				$this->Session->setFlash(__d('store','The picture could not be saved. Please, try again.', true));
			}	
			$this->redirect($this->referer());			
		}
		$this->data = $this->StoreProduct->findById($id);
	}	
	
	function delete($id = null) {
		$gallery = $this->StoreProduct->findById($id);
	    $upload =  $this->StoreUpload->findById($gallery['StoreProduct']['upload_id']);	    
	    if($this->FileUpload->removeFile($upload['StoreUpload']['name'])){
			if($this->StoreUpload->delete($upload['StoreUpload']['id'])){
		      	if($this->StoreProduct->delete($gallery['StoreProduct']['id'])){
		        	$this->Session->setFlash('File deleted');
		      	}else{
		      		$this->Session->setFlash(__d('store','The picture could not be deleted from Gallery. Please, try again.', true));
		      	}
	        	
			}else{
				$this->Session->setFlash(__d('store','The picture could not be deleted from Upload. Please, try again.', true));
			}
	    }else{
	    	$this->Session->setFlash(__d('store','The picture could not be deleted from Disc. Please, try again.', true));
	    }
	    $this->redirect($this->referer());
	}
	
	function delete_gallery($group_id = null) {
		$gallery = $this->StoreProduct->find('list',array(
			'conditions'=>array('StoreProduct.group_id'=>$group_id),
			'fields'=>array('StoreProduct.id','StoreProduct.upload_id')));
		foreach ($gallery as $gallery_id => $upload_id) {
			$upload =  $this->StoreUpload->findById($upload_id);	    
		    if($this->FileUpload->removeFile($upload['StoreUpload']['name'])){
				if($this->StoreUpload->delete($upload['StoreUpload']['id'])){
			      	if($this->StoreProduct->delete($gallery_id)){
			        	$this->Session->setFlash('File deleted');
			      	}else{
			      		$this->Session->setFlash(__d('store','The picture could not be deleted from StoreProduct. Please, try again.', true));
			      	}		        	
				}else{
					$this->Session->setFlash(__d('store','The picture could not be deleted from Upload. Please, try again.', true));
				}
		    }else{
		    	$this->Session->setFlash(__d('store','The picture could not be deleted from Disc. Please, try again.', true));
		    }
		}	    
	    $this->redirect($this->referer());
	}	
	

}
?>